<?php


namespace lib;

use PHPMailer\PHPMailer\PHPMailer;

/**
 * Class Mail   邮件
 * @package app\common\controller
 */
class Mail
{
    // 服务器配置,一般是默认配置
    // 邮件编码
    private $charset = 'utf-8';
    // 调试模式输出
    private $debug = 0;
    // 使用smtp
    private $is_smtp = true;
    // smtp服务器
    private $host = 'smtp.163.com';
    // 允许smtp认证
    private $smtp_auth = true;
    // smtp 用户名
    private $username = 'zpaizw@163.com';
    // smtp 密码
    private $password = 'ZMKJRAYLMAIAISIN';
    // 允许ssl或者tls
    private $smtp_secure = 'ssl';
    // 服务器端口 25或465
    private $port = 465;
    // 发件人邮箱
    private $from = 'zpaizw@163.com';
    // 发件人用户昵称
    private $from_name = '周鹏';
    // 邮件对象
    private $mail = null;

    // 设定初始值
    protected function _initialize()
    {
        $this->charset = config('mail.charset');
        $this->debug = config('mail.debug');
        $this->is_smtp = config('mail.is_smtp');
        $this->host = config('mail.host');
        $this->smtp_auth = config('mail.smtp_auth');
        $this->username = config('mail.username');
        $this->password = config('mail.password');
        $this->smtp_secure = config('mail.smtp_secure');
        $this->port = config('mail.port');
        $this->from = config('mail.from');
        $this->from_name = config('mail.from_name');
    }

    public function __construct()
    {
        // 服务器配置
        $mail = new PHPMailer(true);
        $mail->CharSet = $this->charset;
        $mail->SMTPDebug = $this->debug;
        if ($this->is_smtp === true) {
            $mail->isSMTP();
        }
        $mail->Host = $this->host;
        $mail->SMTPAuth = $this->smtp_auth;
        $mail->Username = $this->username;
        $mail->Password = $this->password;
        $mail->SMTPSecure = $this->smtp_secure;
        $mail->Port = $this->port;
        $mail->setFrom($this->from, $this->from_name);
        $this->mail = $mail;
    }

    /**
     * 发送邮件
     * @param array $sendAddress 收件人地址数组， 如果为单值数组则只写入收件地址，如果为键值数组则还写入收件人名称
     * @param string $Subject 邮件主题
     * @param string $Body 邮件内容，如果以html格式可以写入html
     * @param string $AltBody 如果为单值数组则只写入收件地址，如果为键值数组则还写入收件人名称
     * @param array $Attachment 附件数组，如果为单值数组则为添加附件，如果为键值数组则添加附件并且重命名
     * @param bool $isHtml 是否以html文档格式发送
     * @param array $CC 抄送地址数组， 如果为单值数组则只写入收件地址，如果为键值数组则还写入收件人名称
     * @param array $BCC 密送地址数组，如果为单值数组则只写入收件地址，如果为键值数组则还写入收件人名称
     * @return bool是否发送成功
     */
    public function sendEmail($sendAddress = [], $Subject = '', $Body = '', $AltBody = '', $Attachment = [], $isHtml = true, $CC = [], $BCC = [])
    {
        try {
            // 必填参数校验
            if (empty($sendAddress) || empty($Subject) || empty($Body)) {
                return false;
            }
            // 收件人
            foreach ($sendAddress as $key => $value) {
                if (is_int($key)) {
                    // 处理单值
                    $this->mail->addAddress($value);
                } else {
                    // 处理键值
                    $this->mail->addAddress($value, $key);
                }
            }
            // 附件
            if (!empty($Attachment)) {
                foreach ($Attachment as $key => $value) {
                    if (is_int($key)) {
                        // 处理单值
                        $this->mail->addAttachment($value);
                    } else {
                        // 处理键值
                        $this->mail->addAttachment($value, $key);
                    }
                }
            }
            //抄送
            if (!empty($CC)) {
                //抄送不为空
                foreach ($CC as $key => $value) {
                    if (is_int($key)) {
                        // 处理单值
                        $this->mail->addCC($value);
                    } else {
                        // 处理键值
                        $this->mail->addCC($value, $key);
                    }
                }
            }
            //密送
            if (!empty($BCC)) {
                //密送不为空
                foreach ($BCC as $key => $value) {
                    if (is_int($key)) {
                        // 处理单值
                        $this->mail->addBCC($value);
                    } else {
                        // 处理键值
                        $this->mail->addBCC($value, $key);
                    }
                }
            }
            //回复的时候回复给哪个邮箱 建议和发件人一致
            $this->mail->addReplyTo($this->from, $this->from_name);

            //Content
            // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
            $this->mail->isHTML($isHtml);
            //邮件主题
            $this->mail->Subject = $Subject;
            //邮件内容
            $this->mail->Body = $Body;
            //如果邮件客户端不支持HTML则显示此内容
            $this->mail->AltBody = $AltBody;
            // 忽略ssl认证
            $this->mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );
            //发送
            $this->mail->send();
            return true;
        } catch (\Exception $e) {
            echo '邮件发送失败: ' . $this->mail->ErrorInfo;
            return false;
        }
    }
}